package atm;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * 取款
 */
public class GetPanel extends JPanel implements ActionListener {
    private AtmGUI atmGUI;
    private JLabel moneyLabel = new JLabel("请输入取款金额：");
    private JTextField moneyTextField = new JTextField();
    private Font font = new Font("宋体",Font.BOLD,18);
    private JButton sureBtn = new JButton(new ImageIcon("images\\buttonSure.jpg"));
    private JButton offBtn = new JButton(new ImageIcon("images\\buttonOff.jpg"));
    public GetPanel(AtmGUI atmGUI){
        this.atmGUI = atmGUI;
        this.setLayout(null);
        this.setOpaque(false);
        moneyLabel.setBounds(250,200,200,50);
        moneyLabel.setFont(font);
        this.add(moneyLabel);
        moneyTextField.setBounds(400,200,250,50);
        moneyTextField.setFont(font);
        this.add(moneyTextField);
        sureBtn.setBounds(800,400,140,50);
        sureBtn.addActionListener(this);
        this.add(sureBtn);
        offBtn.setBounds(0,400,140,50);
        offBtn.addActionListener(this);
        this.add(offBtn);

   }
    @Override
    public void actionPerformed(ActionEvent e) {
        ArrayList<User> users = atmGUI.getUsers();
        Double blance = Double.parseDouble(moneyTextField.getText());
        if (e.getSource() == sureBtn){
        for (int i = 0;i < users.size();i++){
            if (blance > users.get(i).getBlance()){
                JOptionPane.showMessageDialog(this,"余额不足");
                return;
            }else {
                JOptionPane.showMessageDialog(this,"取款成功");
                users.get(i).setBlance(users.get(i).getBlance() - blance);
                break;
            }
        }

    }
        moneyTextField.setText("");
        atmGUI.showPanel("menu");
}
}
